# Copyright (c) 2007 Chris Moyer http://coredumped.org
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import os


class Graph:
	"""
	This is a very simple class to wrap the options of RRDtool's Graph
	functionality.
	"""
	def __init__(self,
					 end="now", start="now-1d", step=None,			# Time
					 vertical_label=None, title=None, 				# Labels
					 width=None, height=None,							# Size
					 upper_limit=None,lower_limit=None,				# Limits
					 x_grid=None, y_grid=None,							# Grid
					 color=None, zoom=None, font=None,				# Misc
					 imgformat="PNG",base=None,watermark=None		# Misc
					 ):
		self.arguments = {}
		self.arguments['end'] = end
		self.arguments['start'] = start
		self.arguments['step'] = step
		self.arguments['vertical_label'] = vertical_label
		self.arguments['title'] = title
		self.arguments['width'] = width
		self.arguments['height'] = height
		self.arguments['upper-limit'] = upper_limit
		self.arguments['lower-limit'] = lower_limit
		self.arguments['x-grid'] = x_grid
		self.arguments['y-grid'] = y_grid
		self.arguments['color'] = color
		self.arguments['zoom'] = zoom
		self.arguments['font'] = font
		self.arguments['imgformat'] = imgformat
		self.arguments['base'] = base
		self.arguments['watermark'] = watermark

		self.data = []
		self.graphs = []

	def addData(self, name, value, type="DEF"):
		self.data.append(type+":"+name+"="+value)

	def addGraph(self, name, color, type="LINE", title=None):
		str = type + ":" + name + color
		if(title):
			str = str + ":" + title
		
		self.graphs.append(str)

	def generate(self,filename):
		cmd = filename
		for arg, val in self.arguments.iteritems():
			if(val):
				cmd = cmd + " --" + arg + " " + str(val)

		for data in self.data:
			cmd = cmd + " " + data

		for graph in self.graphs:
			cmd = cmd + " " + graph

		return os.system("rrdtool graph " + cmd + " > /dev/null")
